<?php
/**
 * Copyright (c) 2018,2345
 * 摘    要：分页 类程序
 * 作    者：林幸亿
 * 修改日期：2018.04.24
 */

/*
 * 参考网址
 * https://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
 */

class Paginator 
{
    private $_conn;
    private $_limit;
    private $_page;
    private $_query;
    private $_total;

    /**
     * 功    能：Get the total number of rows.
     * 修改日期：2018-4-24
     */
    public function __construct($conn, $query)
    {
        $this->_conn = $conn;
        $this->_query = $query;
        $rs = $this->_conn->getConnection()->query($this->_query);

        //$this->_total = $rs->num_rows;
        $this->_total = $rs->rowCount();
    }

    /**
     * 功    能：Retrieve rows based on the page and limit
     * 修改日期：2018-4-24
     *
     * @param integer $page
     * @param integer $limit
     * @return object
     */
    public function getData($page = 1, $limit = 10)
    {
        $this->_limit = $limit;
        $this->_page = $page;

        if ($this->_limit == 'all') {
            $query = $this->_query;
        } else {
            $query = $this->_query . " LIMIT " . ( ( $this->_page - 1 ) * $this->_limit ) . ", $this->_limit";
        }

        /*
          $rs = $this->_conn->query($query);
          $rs->setFetchMode(PDO::FETCH_ASSOC);

          while ($row = $rs->fetch()) {
          //while ($row = $rs->fetch_assoc()) {
          $results[] = $row;
          }
         */
        $results = $this->_conn->finds($query);

        $result = new stdClass();
        $result->page = $this->_page;
        $result->limit = $this->_limit;
        $result->total = $this->_total;
        $result->data = $results;

        return $result;
    }

    /**
     * 功    能：Output the HTML pagination link
     * 修改日期：2018-4-24
     *
     * @param integer $links
     * @param string $list_class
     * @return string
     */
    public function createLinks($links, $list_class)
    {
        if ($this->_limit == 'all')
        {
            return '';
        }

        $last = ceil($this->_total / $this->_limit);

        $start = ( ( $this->_page - $links ) > 0 ) ? $this->_page - $links : 1;
        $end = ( ( $this->_page + $links ) < $last ) ? $this->_page + $links : $last;

        $html = '<div class="' . $list_class . '">';
        $html .= '<ul>';

        $class = ( $this->_page == 1 ) ? "disabled" : "";
        $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page - 1 ) . '">&laquo;</a></li>';

        if ($start > 1)
        {
            $html .= '<li><a href="?limit=' . $this->_limit . '&page=1">1</a></li>';
            $html .= '<li class="disabled"><span>...</span></li>';
        }

        for ($i = $start; $i <= $end; $i++)
        {
            $class = ( $this->_page == $i ) ? "active" : "";
            $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . $i . '">' . $i . '</a></li>';
        }

        if ($end < $last)
        {
            $html .= '<li class="disabled"><span>...</span></li>';
            $html .= '<li><a href="?limit=' . $this->_limit . '&page=' . $last . '">' . $last . '</a></li>';
        }

        $class = ( $this->_page == $last ) ? "disabled" : "";
        $html .= '<li class="' . $class . '"><a href="?limit=' . $this->_limit . '&page=' . ( $this->_page + 1 ) . '">&raquo;</a></li>';

        $html .= '</ul>';
        $html .= '</div>';

        return $html;
    }

}
